﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using Gama.Contract;

namespace Gama.Website.Models
{
    [Serializable]
    public class ImageViewModel : ImageDto
    {
        public ImageViewModel()
        {
            this.FileName = string.Empty;
            this.Path = string.Empty;
            this.Caption = string.Empty;
        }

        private static ImageViewModel noImage;

        private static ImageViewModel NoImage
        {
            get
            {
                if (noImage == null)
                {
                    noImage = new ImageViewModel
                    {
                        Caption = "No Image Available",
                        FileName = ConfigurationManager.AppSettings["NoImageFileName"],
                        Path = ConfigurationManager.AppSettings["PathToImages"]
                    };
                }

                return noImage;
            }
        }

        #region ImageManager

        public static ImageViewModel GetResizedImage(ImageDto image, ImageSize imageSize)
        {
            string pathToImage = String.Format("{0}{1}\\{2}\\{3}",
                                                HttpContext.Current.Request.PhysicalApplicationPath,
                                                ConfigurationManager.AppSettings["PathToImages"],
                                                image.Path, image.FileName);

            //if (!File.Exists(pathToImage))
            //{
            //    return NoImage;
            //}

            switch (imageSize)
            {
                case ImageSize.Size75x75:
                    return GetResizedImage(image, 75, 75);

                case ImageSize.Size100x100:
                    return GetResizedImage(image, 100, 100);

                case ImageSize.Size120x120:
                    return GetResizedImage(image, 120, 120);

                case ImageSize.Size128x128:
                    return GetResizedImage(image, 128, 128);

                case ImageSize.Size140x140:
                    return GetResizedImage(image, 140, 140);

                case ImageSize.Size144x144:
                    return GetResizedImage(image, 144, 144);

                case ImageSize.Size204x204:
                    return GetResizedImage(image, 204, 204);

                case ImageSize.Size215x215:
                    return GetResizedImage(image, 215, 215);

                case ImageSize.Size220x220:
                    return GetResizedImage(image, 220, 220);

                case ImageSize.Size264x264:
                    return GetResizedImage(image, 264, 264);

                default:
                    return new ImageViewModel
                    {
                        FileName = image.FileName,
                        Path = image.Path,
                        Caption = image.Caption,
                    };
            }
        }

        private static ImageViewModel GetResizedImage(ImageDto image, int width, int height)
        {
            string extension = System.IO.Path.GetExtension(image.FileName);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(image.FileName);
            string resizedRelativePath = string.IsNullOrEmpty(image.Path) ? ConfigurationManager.AppSettings["ResizedImageFolder"] : string.Format("{0}/{1}", image.Path, ConfigurationManager.AppSettings["ResizedImageFolder"]);
            string resizedFileName = string.Format("{0}-{1}-{2}{3}", fileName, width, height, extension);

            return new ImageViewModel
            {
                Caption = image.Caption,
                Path = string.Format("{0}{1}", ConfigurationManager.AppSettings["PathToImages"], resizedRelativePath),
                FileName = resizedFileName
            };
        }

        #endregion
    }

    #region Nested class

    public enum ImageSize
    {
        Default,
        Size75x75,
        Size100x100,
        Size120x120,
        Size128x128,
        Size140x140,
        Size144x144,
        Size204x204,
        Size215x215,
        Size220x220,
        Size264x264,
    }

    #endregion
}
